[math] Make TRandom compatible with UniformRandomBitGenerator - Issue # 7979#22508
Conversation
TRandom and its subclasses can now be used directly with standard library algorithms like std::shuffle and std::uniform_int_distribution. This is done by adding the three required members to TRandom: result_type, min(), max(), and operator(). The operator() delegates to the virtual Rndm(), so polymorphism is preserved across all TRandom subclasses. Fixes root-project#7979
|
also to discuss whether this can be unified with tmva/tmva/inc/TMVA/Tools.h to avoid code duplicities |
- Convert // NOTE: comment to Doxygen /// \note format in TRandom.h - Make TRandom::operator()() virtual so subclasses can override it - Add TRandom2::operator()() returning raw Tausworthe XOR output, avoiding the double round-trip through Rndm() / kScale - Add TRandom3::operator()() returning raw Mersenne Twister output, same motivation as above - Extend testMathRandom test5() to exercise TRandom2 as a URBG TRandom1 (RANLUX) has a float-based internal state with no meaningful raw integer path, so it keeps the base-class fallback. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks for the review! Addressed all three points:
For TRandom1 (RANLUX), the internal state is float-based (fFloatSeedTable), so there's no raw integer to expose — the base class fallback is the best we can do there. |
The main difference is that our approach embeds the interface directly in TRandom, so any existing TRandom* pointer works as a URBG without wrapping. It also lets subclasses override operator()() to return raw integers directly (as done here for TRandom2 and TRandom3), which TMVA::RandomGenerator doesn't do, it goes through Integer() which still uses a double internally. i'm open to explore that in a follow-up PR if that's the preferred direction. |
Avoids duplicating the Tausworthe/Mersenne Twister state-advance logic. Rndm() now calls operator()() to get the raw integer and then scales it to a double, keeping a single source of truth for the generator core. The TAUSWORTHE macro is moved to file scope so RndmArray can still use it.
|
@guitargeek @hageboeck hey guys! could you check this pr? please |
Test Results 22 files 22 suites 3d 16h 35m 49s ⏱️ For more details on these failures, see this check. Results for commit b59d5c7. |
guitargeek
left a comment
There was a problem hiding this comment.
Thank you very much for this feature and to @ferdymercury for the review!
TRandom and its subclasses can now be used directly with standard library algorithms like std::shuffle and std::uniform_int_distribution.
This is done by adding the three required members to TRandom: result_type, min(), max(), and operator(). The operator() delegates to the virtual Rndm(), so polymorphism is preserved across all TRandom subclasses.
Fixes #7979
This Pull request:
Changes or fixes:
Checklist:
This PR fixes #